using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BM_AppDataManager : MonoBehaviour
{
	
	#region Members
	
	private int						m_iCurrentLevel = 0;
	private int						m_iCurrentWorld = 0;

	#endregion
	
	
	#region Properties
	
	public int CurrentLevel
	{
		get { return m_iCurrentLevel; }
	}
	
	public int CurrentWorld
	{
		get { return m_iCurrentWorld; }
	}
	
	public int WorldsCount
	{
		get { return SV_WorldsLevelsData.m_WorldsData.Length; }
	}
	
	public bool SoundStatus_OnFile
	{
		get
		{
			bool bRVal = true;
			
			string strSoundEntry = BM_Common.GetLabelName(StringLabel.LABEL_FILE_AUDIO_STATUS);
			
			if(PlayerPrefs.HasKey(strSoundEntry))
				bRVal = (PlayerPrefs.GetInt(strSoundEntry) > 0);
			else
				PlayerPrefs.SetInt(strSoundEntry, 1);
			
//			if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//				Debug.Log(GetType()+".SoundStatus_OnFile -> LoadedSoundStatus: " + bRVal);
			
			return bRVal;
		}
		
		set
		{
			PlayerPrefs.SetInt(BM_Common.GetLabelName(StringLabel.LABEL_FILE_AUDIO_STATUS), (value ? 1 : 0));
			
//			if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//				Debug.Log(GetType()+".SoundStatus_OnFile -> SavedSoundStatus: " + value);
		}
	}
	
	public bool Firstplay_OnFile
	{
		get
		{
			bool bRVal = true;
			
			string strFirstplayEntry = BM_Common.GetLabelName(StringLabel.LABEL_FILE_FIRSTPLAY_STATUS);
			
			if(PlayerPrefs.HasKey(strFirstplayEntry))
				bRVal = (PlayerPrefs.GetInt(strFirstplayEntry) > 0);
			else
				PlayerPrefs.SetInt(strFirstplayEntry, 0);
			
//			if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//				Debug.Log(GetType()+".Firstplay_OnFile -> LoadedFirstplayStatus: " + bRVal);
			
			return bRVal;
		}
		
		set
		{
			PlayerPrefs.SetInt(BM_Common.GetLabelName(StringLabel.LABEL_FILE_FIRSTPLAY_STATUS), (value ? 1 : 0));
			
//			if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//				Debug.Log(GetType()+".Firstplay_OnFile -> SavedFirstplayStatus: " + value);
		}
	}
	
	#endregion
	
	public void Awake()
	{
		m_rManagerReference = this;
	}
	
	public void Start()
	{
		SetCurrentWorldLevel(0, 0);

//		LoadLevelsData();
	}
	
	public void OnPause()
	{
//		SaveLevelsData();
	}
	
	public void OnDestroy()
	{
		m_rManagerReference = null;

//		SaveLevelsData();
	}

	
	#region Tour Management
	
	#endregion
	
	
	#region Info
	
	public int GetLevelsCount(int iWorld)
	{
		int iRVal = 0;
		
		if(iWorld >= 0 && iWorld < WorldsCount)
			iRVal = SV_WorldsLevelsData.m_LevelsData[iWorld].Length;
		
		return iRVal;
	}
	
	public bool SetCurrentWorldLevel(int iWorld, int iLevel)
	{
		bool bRVal = false;
		
		int iLevelsCount = GetLevelsCount(iWorld);
		if(iLevel >= 0 && iLevel < iLevelsCount)
		{
//			m_eRestartLevel = RestartType.RESTART_NONE;
			m_iCurrentWorld = iWorld;
			m_iCurrentLevel = iLevel;
			bRVal = true;
		}
		else if(Debug.isDebugBuild)
		{
			Debug.LogError("CM_AppDataManager.SetCurrentWorldLevel: numero livello o mondo non valido!");
			Debug.DebugBreak();
		}
				
		return bRVal;	
	}
	
	#endregion
	
	
	#region Persistent Data Management
	
//	public void LoadLevelsData()
//	{
//		if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//			Debug.Log("####################   START LOADING LEVELS DATA   ########################");
//		
//		m_dLevelData.Clear();
//		
//		for(int iLevel = 0; iLevel < (int)LevelName.LEVEL_COUNT; ++iLevel)
//		{
//			string strLevelName = ((LevelName)iLevel).ToString();
//			
//			int iLevelCount = 0;
//			string strLevelCount = strLevelName+GP_Common.GetLabelName(StringLabel.LABEL_FILE_COUNT);
//			if(PlayerPrefs.HasKey(strLevelCount))
//				iLevelCount = PlayerPrefs.GetInt(strLevelCount);
//			else
//				PlayerPrefs.SetInt(strLevelCount, iLevelCount);
//			
//			LevelData rLevelData = new LevelData();
//			rLevelData.m_eLevelName = (LevelName)iLevel;
//			rLevelData.m_iPlayCount = iLevelCount;
//			m_dLevelData.Add(rLevelData.m_eLevelName, rLevelData);
//			
//			if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//				Debug.Log("Level Name: " + rLevelData.m_eLevelName + " - Count: " + rLevelData.m_iPlayCount);
//		}
//		
//		PlayerPrefs.Save();
//		
//		if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//			Debug.Log("####################   END LOADING LEVELS DATA   ########################");
//	}
	
//	public void SaveLevelsData()
//	{
//		if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//			Debug.Log("####################   START SAVING LEVELS DATA   ########################");
//
//		foreach(KeyValuePair<LevelName, LevelData> rKeyValue in m_dLevelData)
//		{
//			string strLevelCount = rKeyValue.Key.ToString()+GP_Common.GetLabelName(StringLabel.LABEL_FILE_COUNT);
//			PlayerPrefs.SetInt(strLevelCount, rKeyValue.Value.m_iPlayCount);
//			
//			if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//				Debug.Log("Level Name: " + rKeyValue.Value.m_eLevelName + " - Count: " + rKeyValue.Value.m_iPlayCount);
//		}
//		
//		if(GP_GameConfig.Instance().PrintLevelsDataOutput)
//			Debug.Log("####################   END SAVING LEVELS DATA   ########################");
//		
//		PlayerPrefs.Save();
//	}
	
	#endregion
	
	
	#region Singleton
	
	private static BM_AppDataManager		m_rManagerReference = null;
	
	public static BM_AppDataManager Instance()
	{
		if(m_rManagerReference == null)
		{
			Debug.LogError("BM_AppDataManager.Instance() -> Manager non inizializzato! ERRORE!");
			Debug.DebugBreak();
		}
		
		return m_rManagerReference;
	}
	
	#endregion
}
